package com.klasster.recruitmentPlatform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.common.util.TokenUtil;
import com.klasster.recruitmentPlatform.domain.dto.UserDTO;
import com.klasster.recruitmentPlatform.domain.po.Recruiter;
import com.klasster.recruitmentPlatform.domain.po.Seeker;
import com.klasster.recruitmentPlatform.domain.po.User;
import com.klasster.recruitmentPlatform.service.impl.RecruiterServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.SeekerServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 用户登录注册相关操作
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthController {

    private final UserServiceImpl userService;
    private final SeekerServiceImpl seekerService;
    private final RecruiterServiceImpl recruiterService;
    private final TokenUtil tokenUtil;

    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        //先判断用户名是否已存在
        if(userService.getByUsername(userDTO.getUsername()) != null) {
            return Result.error(ResultCode.AUTH_USERNAME_EXIST,false);
        }
        User user = BeanUtil.copyProperties(userDTO, User.class);
        user.setAvatar_url("https://image0.lietou-static.com/img/5f8fa3a6f6d1ab58476f322808u.png");
        boolean save = userService.save(user);
        if(save) {
            Long id = userService.getByUsername(userDTO.getUsername()).getUser_id();
            if("seeker".equals(userDTO.getRole())) {
                Seeker seeker = new Seeker();
                seeker.setSeeker_id(id);
                seekerService.save(seeker);
            }
            else {
                Recruiter recruiter = new Recruiter();
                recruiter.setRecruiter_id(id);
                recruiterService.save(recruiter);
            }
            return Result.success(ResultCode.AUTH_REGISTER_SUCCESS,true);
        }
        else {
            return Result.error(ResultCode.AUTH_REGISTER_FAIL,false);
        }
    }


    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        //先判断用户名是否已存在
        User user = userService.getByUsername(userDTO.getUsername());
        if(user == null) {
            return Result.error(ResultCode.AUTH_USERNAME_NOT_EXIST,false);
        }
        else {
            if(userDTO.getPassword().equals(user.getPassword())) {
                String token = tokenUtil.generateToken(user.getUser_id(),user.getPassword());
                return Result.success(ResultCode.AUTH_LOGIN_SUCCESS,token);
            }
            else {
                return Result.error(ResultCode.AUTH_LOGIN_FAIL,false);
            }
        }
    }

}
